package cool.tianyou.blue.bird.common.util;

import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class JsonReturn {
    private Integer code;
    private String msg;
    private Object body;

    public JsonReturn setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public static JsonReturn ope(){
        JsonReturn jsonReturn = new JsonReturn();
        jsonReturn.setCode(105);
        return jsonReturn;
    }

    public static JsonReturn ope(String msg,Object body){
        JsonReturn ope = ope();
        ope.setBody(body);
        ope.setMsg(msg);
        return ope;
    }


    private Map<String,Object> map;

    public JsonReturn(String msg,Integer code){
        this.map = new HashMap<>();
        this.msg = msg;
        this.code = code;
    }

    public JsonReturn put(String key,Object value) {
        if(this.map==null){
            this.map = new HashMap<>();
        }
        this.map.put(key, value);
        return this;
    }

    public static JsonReturn ok(){
        return new JsonReturn("success",200);
    }
    public static JsonReturn error(){
        return new JsonReturn("error",100);
    }

    public static JsonReturn error(String msg,Integer code){
        return new JsonReturn(msg,code);
    }
    public static JsonReturn error(String msg){
        return new JsonReturn(msg,100);
    }

    public String toJson(){
        return JSONObject.toJSONString(this);
    }

}
